package org.jeecg.modules.device.com.pudong.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.jeecg.modules.device.com.pudong.entity.DataAhl;
import org.jeecg.modules.device.com.pudong.entity.PudongDeviceData;
import org.jeecg.modules.device.com.pudong.mapper.DataAhlMapper;
import org.jeecg.modules.device.com.pudong.mapper.PudongDeviceDataMapper;
import org.jeecg.modules.device.com.pudong.service.IDataAhlService;
import org.jeecg.modules.device.com.pudong.service.IPudongDeviceDataService;
import org.jeecg.modules.device.com.pudong.vo.PudongDeviceDataPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: pudong_device_data
 * @Author: jeecg-boot
 * @Date:   2024-06-11
 * @Version: V1.0
 */
@Service
public class PudongDeviceDataServiceImpl extends ServiceImpl<PudongDeviceDataMapper, PudongDeviceData> implements IPudongDeviceDataService {

	@Autowired
	private PudongDeviceDataMapper pudongDeviceDataMapper;
	@Autowired
	private DataAhlMapper dataAhlMapper;

	@Autowired
	private IDataAhlService dataAhlService;
	
	@Override
	@Transactional
	public void saveMain(PudongDeviceData pudongDeviceData, List<DataAhl> dataAhlList) {
		pudongDeviceDataMapper.insert(pudongDeviceData);
		if(dataAhlList!=null && dataAhlList.size()>0) {
			for(DataAhl entity:dataAhlList) {
				//外键设置
				entity.setDataId(pudongDeviceData.getId());
				dataAhlMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(PudongDeviceData pudongDeviceData,List<DataAhl> dataAhlList) {
		pudongDeviceDataMapper.updateById(pudongDeviceData);
		
		//1.先删除子表数据
		dataAhlMapper.deleteByMainId(pudongDeviceData.getId());
		
		//2.子表数据重新插入
		if(dataAhlList!=null && dataAhlList.size()>0) {
			for(DataAhl entity:dataAhlList) {
				//外键设置
				entity.setDataId(pudongDeviceData.getId());
				dataAhlMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		dataAhlMapper.deleteByMainId(id);
		pudongDeviceDataMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dataAhlMapper.deleteByMainId(id.toString());
			pudongDeviceDataMapper.deleteById(id);
		}
	}

	@Override
	public PudongDeviceDataPage analysisData(String id) {
		PudongDeviceDataPage pudongDeviceDataPage = new PudongDeviceDataPage();
		PudongDeviceData pudongDeviceData = pudongDeviceDataMapper.selectById(id);
		//上报时间 翻译
		long timestamp = Long.parseLong(pudongDeviceData.getTimestamp());
		pudongDeviceData.setTimestampDict(new Date(timestamp));
		//采集时间 翻译
		long tt= Long.parseLong(pudongDeviceData.getTt());
		pudongDeviceData.setTtDict(new Date(tt));
		String afdb = pudongDeviceData.getAfdb();
		if (afdb.length()==4){
			String afdb_dict = getUtf8Time(afdb);
			pudongDeviceData.setAfdbDict(afdb_dict);

		}
		String afaf = pudongDeviceData.getAfaf();
		if (afaf.length()==4) {
			String afaf_dict = getUtf8Time(afaf);
			pudongDeviceData.setAfafDict(afaf_dict);

		}
		String aaab = pudongDeviceData.getAaab();
		if (aaab.length()==4){
			String aaab_dict = getUtf8Time(aaab);
			pudongDeviceData.setAaabDict(aaab_dict);
		}
		String aaad = pudongDeviceData.getAaad();
		if (aaab.length()==4){
			String aaad_dict = getUtf8Time(aaad);
			pudongDeviceData.setAaadDict(aaad_dict);

		}

		String agad = pudongDeviceData.getAgad();

		if (agad.length()==4){
			String agad_dict = getUtf8Time(agad);
			pudongDeviceData.setAgadDict(agad_dict);

		}

		String agab = pudongDeviceData.getAgab();

		if (agab.length()==4){
			String agab_dict = getUtf8Time(agab);
			pudongDeviceData.setAgabDict(agab_dict);

		}

		String adad = pudongDeviceData.getAdad();

		if (adad.length()==4){
			String adad_dict = getUtf8Time(adad);
			pudongDeviceData.setAdadDict(adad_dict);

		}


		String str = pudongDeviceData.getAhl1();

//		dataAhlMapper.deleteByMainId(id);
		int chunkSize = 2;
		int numChunks = str.length()  / chunkSize;
		List<DataAhl> dataAhls = new LinkedList<>();
		for (int i = 0; i < numChunks; i++) {
			int start = i * chunkSize;
			int end = Math.min(start + chunkSize, str.length());
			String chunk = str.substring(start, end);
			DataAhl entity = new DataAhl();
			entity.setDataId(id);
			String time = String.valueOf((i+1));
			entity.setMinute("第"+time+"分钟");
			entity.setValue(chunk);
			dataAhls.add(entity);

		}
//		dataAhlService.saveBatch(dataAhls);
		this.updateById(pudongDeviceData);
		BeanUtils.copyProperties(pudongDeviceData,pudongDeviceDataPage);

		pudongDeviceDataPage.setDataAhlList(dataAhls);
		return pudongDeviceDataPage;
	}


	public String getUtf8Time(String str) {
		return str.substring(0, 2) + "时" + str.substring(2, 4) + "分";
	}
}
